package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.CabinetErrorDto;
import com.be.beadmin.quality.dto.input.CabinetQualityAndCabinetErrorDto;
import com.be.beadmin.quality.dto.input.CabinetQualityDto;
import com.be.beadmin.quality.dto.output.CabinetQualityEx;
import com.be.beadmin.quality.dto.output.CabinetQualityExcelEx;
import com.be.beadmin.quality.entity.CabinetError;
import com.be.beadmin.quality.service.CabinetQualityService;
import com.be.beadmin.service.dto.output.FactoryCalendarItemEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author zjm
* @date 2023-05-15
**/
@RestController
public class CabinetQualityController {

    @Autowired
    private  CabinetQualityService cabinetQualityService;


    @PostMapping(value = "/quality/getCabinetQualityByPage")
    public ResultModel getCabinetQualityByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(cabinetQualityService.getCabinetQualityByPage(pageEntity)) ;
    }

    @PostMapping("/quality/addCabinetQuality")
    public ResultModel addCabinetQuality(@RequestBody CabinetQualityDto cabinetQualityDto){
        cabinetQualityService.addCabinetQuality(cabinetQualityDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation(value = "新增接线主表和错误子表")
    @PostMapping("/quality/addCabinetQualityAndCabinetError")
    public ResultModel addCabinetQualityAndCabinetError(@RequestBody CabinetQualityAndCabinetErrorDto cabinetQualityAndCabinetErrorDto){
        CabinetQualityDto cabinetQualityDto = cabinetQualityAndCabinetErrorDto.getCabinetQualityDto();
        CabinetErrorDto[] cabinetErrorDtos = cabinetQualityAndCabinetErrorDto.getCabinetErrorDtos();
        cabinetQualityService.addCabinetQualityAndCabinetError(cabinetQualityDto, cabinetErrorDtos);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation(value = "编辑接线主表和错误子表")
    @PostMapping("/quality/editCabinetQualityAndCabinetError")
    public ResultModel editCabinetQualityAndCabinetError(@RequestBody CabinetQualityAndCabinetErrorDto cabinetQualityAndCabinetErrorDto){
        CabinetQualityDto cabinetQualityDto = cabinetQualityAndCabinetErrorDto.getCabinetQualityDto();
        CabinetErrorDto[] cabinetErrorDto = cabinetQualityAndCabinetErrorDto.getCabinetErrorDtos();
        cabinetQualityService.editCabinetQualityAndCabinetError(cabinetQualityDto, cabinetErrorDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/delCabinetQuality")
    public ResultModel delCabinetQuality(@RequestBody CabinetQualityDto cabinetQualityDto){
        cabinetQualityService.delCabinetQuality(cabinetQualityDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateCabinetQuality")
    public ResultModel updateCabinetQuality(@RequestBody CabinetQualityDto cabinetQualityDto){
        cabinetQualityService.updateCabinetQuality(cabinetQualityDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportCabinetQuality")
    public void exportCabinetQuality(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<CabinetQualityEx> cabinetQualityPage = cabinetQualityService.getCabinetQualityByPage(pageEntity);
        List<CabinetQualityEx> cabinetQualityExes = cabinetQualityPage.getContent();
        List<CabinetQualityExcelEx> cabinetQualityExcelExes=cabinetQualityExes.stream().map(item->{
            CabinetQualityExcelEx excel=new CabinetQualityExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("CabinetQuality接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), CabinetQualityExcelEx.class).sheet("sheet1").doWrite(cabinetQualityExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), CabinetQualityExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(cabinetQualityExcelExes);
    }


    @ApiOperation("根据工单号获取接线质量填报相关的信息")
    @PostMapping(value = "/quality/getCabinetQualityInformationByWo")
    public ResultModel getCabinetQualityInformationByWo(@RequestParam String wo){
        return ResultModel.success(cabinetQualityService.getCabinetQualityInformationByWo(wo));
    }
}